import axios from 'axios';


export async function getCurrList() {

  try {

    //先去缓存找，看有没有

    const response = await axios('/api/curriculum/list');   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    //把结果存到缓存

    return result.data
  } catch (error) {
    console.error('Error fetching data:', error);

    return null
  }


}
const tableNames = ['users', 'articles', 'products', 'movies', 'comments', 'categories']

//根据tableNames，定义一个枚举类型，用大写
export enum TableName {

  Articles = 1,
  Products = 2,
  Movies = 3,
  Comments = 4,
  Categories = 5,
  Courses = 6,
  SubItems = 7,
}

export async function getList(typeIndex = 1, articleid = 0,replyType='文章') {

  try {

    //先去缓存找，看有没有
    let url = '/api/demo/getallitems?typeIndex=' + typeIndex
    if (articleid) {
      url += '&articleid=' + articleid+'&replyType='+replyType
    }
    const response = await axios(url);   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    return result
  } catch (error) {
    console.error('Error fetching data:', error);

    return null
  }


}

export async function getSingle(id, typeIndex = 1) {

  try {

    //先去缓存找，看有没有

    const response = await axios('/api/demo/getsingle?id=' + id + '&typeIndex=' + typeIndex);   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    return result
  } catch (error) {
    console.error('Error fetching data:', error);

    return null
  }


}
export async function login(data) {
  try {

    //先去缓存找，看有没有

    const response = await axios.post('/api/demo/login', data) //axios('/api/curriculum/list',);   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    //把结果存到缓存
    if (result.errno == 0)
      return result.data
    else
      return null
    //return  result.data
  } catch (error) {
    console.error('Error fetching data:', error);

    return null
  }

}


export async function reg(data) {
  try {

    //先去缓存找，看有没有

    const response = await axios.post('/api/demo/reg', data) //axios('/api/curriculum/list',);   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    //把结果存到缓存
    
    return  result
  } catch (error) {
    console.error('Error fetching data:', error);

    return false
  }

}


export async function add(data) {
  try {

    //先去缓存找，看有没有

    const response = await axios.post('/api/demo/additem', data) //axios('/api/curriculum/list',);   // '/api/todo/getallitems'     //'http://10.1.6.130/api/curriculum/list')   //

    //const result = await response.json();
    const result = response.data

    //把结果存到缓存
    if (result.errno == 0)
      return true
    else
      return false
    //return  result.data
  } catch (error) {
    console.error('Error fetching data:', error);

    return false
  }

}
export async function update(data) {
  try {

    //先去缓存找，看有没有,jwt

    const response = await axios.post('/api/todo/updateitem', data)

    //const result = await response.json();
    const result = response.data

    //把结果存到缓存
    if (result.errno == 0)
      return true
    else
      return false
    //return  result.data
  } catch (error) {
    console.error('Error fetching data:', error);

    return false
  }

}